﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Xml;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;

using MichaelKappel.Net.HelloWorld.NetNamedPipeBinding;
using MichaelKappel.Net.HelloWorld.NetNamedPipeBinding.Interfaces;

namespace NetNamedPipeBindingHost
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
        }
        private ServiceHost serviceHost;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = TestNotify.Instance;
            TestNotify.Instance.PropertyChanged += (epc, opc) =>
            {
                //Add stuff here
            };
        }
        private void Start()
        {
            this.Background = Brushes.Red;
            try
            {
                string address = "net.pipe://localhost/HelloWorld/NetNamedPipeBindingHost";
                serviceHost = new ServiceHost(typeof(TestService));
                NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
                serviceHost.AddServiceEndpoint(typeof(ITestService), binding, address);
                serviceHost.Open();

                this.Background = Brushes.LightGreen;
                txtHostOnline.Text = "Online :)";

            }
            catch (Exception ex)
            {
                this.Background = Brushes.Red;
                txtHostOnline.Text = ex.Message;
            }
        }
        private void Stop()
        {
            if (serviceHost != null)
                serviceHost.Close();

            this.Background = Brushes.Gray;
            txtHostOnline.Text = "Offline :(";
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            btnStart.IsEnabled = true;
            btnStop.IsEnabled = false;
            Stop();
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            btnStart.IsEnabled = false;
            btnStop.IsEnabled = true;
            Start();
        }

        private void Window_Unloaded(object sender, RoutedEventArgs e)
        {
            Stop();
        }
    }
}

